package com.onpositive.wikistorage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class PropertyStats {
	
	public static int TYPE_FIELD_LENGTH = 10 ;
	public static int FILENAME_FIELD_LENGTH = 40 ;
	
	private String propertyName ;
	private int filesCount = 0 ;
	private HashMap<String,Integer> typeMap = new HashMap<String, Integer>() ;
	//private LinkedHashSet<String> undefSet = new LinkedHashSet<String>();
	ArrayList<String> undefFilesArray = new ArrayList<String>() ;
	ArrayList<String> undefValuesArray = new ArrayList<String>() ;
	
	public PropertyStats( String propertyName )
	{
		this.propertyName = propertyName ;		
	}
	
	public void incrementFilesCount(){
		filesCount++ ;
	}
	
	public HashMap<String, Integer> getTypeMap() {
		return typeMap;
	}

	public void incrementTypeUsageCount( String type ){
		
		Integer usageCount = typeMap.get(type) ;
		if( usageCount == null )
			usageCount = 0 ;
		
		usageCount++ ;
		typeMap.put( type, usageCount ) ;
	}
	
	public void addUndefFile( String fileName , String value )
	{
		//undefSet.add( fileName ) ;
		this.undefFilesArray.add(fileName);
		this.undefValuesArray.add(value) ;
	}
	
	public void flushStats( FileOutputStream stream )
	{
		String outputString =  propertyName + "  " + filesCount + '\n' ;
		
		for( String keyString : typeMap.keySet() )
		{
			outputString += keyString ; 
			int l = keyString.length() ;
			if( TYPE_FIELD_LENGTH > l )
				for( int i = 0 ; i < TYPE_FIELD_LENGTH - l ; i++ )
					outputString += ' ' ;
			
			outputString += typeMap.get(keyString) ;
			outputString += '\n' ;
		}
		outputString += "\n====================================\n\n" ;
			
		try {
			stream.write( outputString.getBytes( "UTF-8" ) ) ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void flushUndefFiles( String path )
	{
//		ArrayList<String> list = new ArrayList<String>() ;
		int s = this.undefFilesArray.size() ;
		if( s == 0 )
			return ;
		try {
			File f = new File( path + "undef_" + propertyName + ".txt" ) ;
			f.createNewFile() ;
			FileOutputStream outputStream = new FileOutputStream( f );
			String unsortedString = "" ;
//			for( String s : undefSet )
//			{
//				unsortedString += ( s + '\n' ) ;
//				list.add(s) ;
//			}
//			Collections.sort( list ) ;
//			
//			String sortedString = "sorted:\n\n" ;
//			for( String s : list )
//				sortedString += ( s + '\n' ) ;
//			
//			sortedString += ( "\n\n"+"unsorted:\n\n" ) ;
//			sortedString += unsortedString ;
//				
//			outputStream.write( sortedString.getBytes( "UTF-8" ) ) ;
			
			
			
			String str = "" ;
			for( int i = 0 ; i < s ; i++ ){
				
				str += this.undefFilesArray.get(i) ;
				
				int l = this.undefFilesArray.get(i).length() ;
				if ( FILENAME_FIELD_LENGTH > l )
					for( int j = 0 ; j < FILENAME_FIELD_LENGTH-l ; j++ )
						str += ' ' ;
				
				else
					str += "   " ;
				
				str += this.undefValuesArray.get(i) ;
				str+='\n' ;
			}
			                               
			outputStream.write( str.getBytes( "UTF-8" ) ) ;			
			
			outputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return ;
		} catch (IOException e) {
			e.printStackTrace();
			return ;
		}
			
	}

	public int getUseCount() {
		return this.filesCount;
	}

}
